package com.sxhuayuan.parking.entity;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.*;

import com.alibaba.druid.util.StringUtils;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sxhuayuan.parking.config.Platform;

/**
 * 车主用户表
 */
@Entity
@Table(name = "hypk_member")
public class Member extends BaseEntity {

	public static final String PRINCIPAL_ATTRIBUTE_NAME = "member";

	private static final long serialVersionUID = -1767473439008389337L;

	/**
	 * 性别
	 */
	public enum Gender {
		unkown,

		/** 男 */
		male,

		/** 女 */
		female;

		public static Gender fromWx(Integer i) {
			switch (i) {
			case 1:
				return Gender.male;
			case 2:
				return Gender.female;
			default:
				return Gender.unkown;
			}
		}

		public static Gender fromAli(String s) {
			if (StringUtils.isEmpty(s)) {
				return Gender.unkown;
			}
			switch (s) {
			case "M":
				return Gender.male;
			case "F":
				return Gender.female;
			default:
				return Gender.unkown;
			}
		}
	}

	private Platform platform;

	private String appid;
	// openId
	private String openid;
	// unionid
	private String unionid;
	// 昵称
	private String nickname;
	// 性别
	private Gender gender;
	// 省份
	private String province;
	// 城市
	private String city;
	// 国家
	private String country;
	// 用户头像
	private String headimg;
	// 消费合计
	private BigDecimal amount;

	/** 用户发票抬头 单向一对一关系 */
	private InvoiceTitle invoiceTitle;

	// 车牌号 一个用户对应多个车牌号
	private Set<MemberCarPlate> carPlates = new HashSet<>();

	public Platform getPlatform() {
		return platform;
	}

	public void setPlatform(Platform platform) {
		this.platform = platform;
	}

	@Column(length = 64, unique = true)
	public String getOpenid() {
		return openid;
	}

	public void setOpenid(String openid) {
		this.openid = openid;
	}

	@Column(length = 64)
	public String getUnionid() {
		return unionid;
	}

	public void setUnionid(String unionid) {
		this.unionid = unionid;
	}

	@Column(length = 64)
	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	@Column(length = 64)
	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	@Column(length = 64)
	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	@Column(length = 64)
	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getHeadimg() {
		return headimg;
	}

	public void setHeadimg(String headimg) {
		this.headimg = headimg;
	}

	@Column(scale = 2, precision = 10)
	public BigDecimal getAmount() {
		return amount;
	}

	public void setAmount(BigDecimal amount) {
		this.amount = amount;
	}

	@JsonIgnore
	@OneToMany(mappedBy = "member", fetch = FetchType.LAZY)
	public Set<MemberCarPlate> getCarPlates() {
		return carPlates;
	}

	public void setCarPlates(Set<MemberCarPlate> carPlates) {
		this.carPlates = carPlates;
	}

	public Gender getGender() {
		return gender;
	}

	public void setGender(Gender gender) {
		this.gender = gender;
	}

	@Override
	public String toString() {
		return new StringBuilder().append("id=").append(getId()).append(",name=").append(getNickname()).append(",openid=").append(getOpenid()).toString();
	}

	@Column(length = 32)
	public String getAppid() {
		return appid;
	}

	public void setAppid(String appid) {
		this.appid = appid;
	}

	@OneToOne
	@JoinColumn(name = "invoice_title_id")
	public InvoiceTitle getInvoiceTitle() {
		return invoiceTitle;
	}

	public void setInvoiceTitle(InvoiceTitle invoiceTitle) {
		this.invoiceTitle = invoiceTitle;
	}
}
